<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增入库单')"/>
    <th:block th:include="include :: datetimepicker-css"/>
    <style type="text/css">
        table label.error {
            position: inherit;
        }

        select + label.error {
            z-index: 1;
            right: 40px;
        }

        .fixed-table-body {
            overflow-x: visible !important;
            overflow-y: visible !important;
            height: 100%;
        }
        .tc {
            text-align: center;
        }
    </style>
</head>
<body class="gray-bg">
<div class="main-content">
    <form id="form-add" class="form-horizontal">
        <h4 class="form-header h4">表单信息</h4>
        <div class="row">
            <div class="col-sm-3">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">入库单号：</label>
                    <div class="col-sm-8">
                        <input name="entryOrderId" class="form-control" type="text" readonly th:value="${entryOrderId}">
                    </div>
                </div>
            </div>
            <div class="col-sm-3">
                <div class="form-group">
                    <label class="col-sm-4 control-label">采购部门：</label>
                    <div class="col-sm-8">
                        <input name="procurementDepartment" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-sm-3">
                <div class="form-group">
                    <label class="col-sm-4 control-label">采购经办人：</label>
                    <div class="col-sm-8">
                        <input name="purchaser" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-sm-3">
                <div class="form-group">
                    <label class="col-sm-4 control-label">入库日期：</label>
                    <div class="col-sm-8">
                        <div class="input-group date">
                            <input name="entryTime" class="form-control" placeholder="yyyy-MM-dd" type="text">
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-3">
                <div class="form-group">
                    <label class="col-sm-4 control-label">仓  库：</label>
                    <div class="col-sm-8">
                        <input name="warehouse" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-sm-3">
                <div class="form-group">
                    <label class="col-sm-4 control-label">供应商：</label>
                    <div class="col-sm-8">
                        <select name="supplierId" class="supplier-select"></select>
                    </div>
                </div>
            </div>
            <div class="col-sm-3">
                <div class="form-group">
                    <label class="col-sm-4 control-label">到货单号：</label>
                    <div class="col-sm-8">
                        <input name="deliveryId" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-sm-3">
                <div class="form-group">
                    <label class="col-sm-4 control-label">使用部门：</label>
                    <div class="col-sm-8">
                        <input name="userDepartment" class="form-control" type="text">
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-3">
                <div class="form-group">
                    <label class="col-sm-4 control-label">合同号：</label>
                    <div class="col-sm-8">
                        <input name="contractId" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-sm-3">
                <div class="form-group">
                    <label class="col-sm-4 control-label">业务员：</label>
                    <div class="col-sm-8">
                        <input name="salesman" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-sm-3">
                <div class="form-group">
                    <label class="col-sm-4 control-label">工程项目：</label>
                    <div class="col-sm-8">
                        <input name="project" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-sm-3">
                <div class="form-group">
                    <label class="col-sm-4 control-label">工程项目：</label>
                    <div class="col-sm-8">
                        <input name="project" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-sm-3">
                <div class="form-group">
                    <label class="col-sm-4 control-label">审核员：</label>
                    <div class="col-sm-8">
                        <input name="reviewedBy" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-sm-3">
                <div class="form-group">
                    <label class="col-sm-4 control-label">仓管员：</label>
                    <div class="col-sm-8">
                        <input name="storekeeper" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-sm-3">
                <div class="form-group">
                    <label class="col-sm-4 control-label">仓库主管：</label>
                    <div class="col-sm-8">
                        <input name="supervisor" class="form-control" type="text">
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-12">
                <div class="form-group">
                    <label class="col-xs-1 control-label">备注：</label>
                    <div class="col-xs-11">
                        <textarea name="notes" maxlength="500" class="form-control" rows="3"></textarea>
                    </div>
                </div>
            </div>
        </div>
        <h4 class="form-header h4">物料信息</h4>
        <div class="row">
            <div class="col-sm-12">
                <button type="button" class="btn btn-white btn-sm" onclick="addRow()"><i class="fa fa-plus"> 增加</i>
                </button>
                <button type="button" class="btn btn-white btn-sm" onclick="sub.delRow()">
                    <i class="fa fa-minus">删除</i></button>
                <div class="col-sm-12 select-table table-striped">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>
    </form>
</div>

<div class="row">
    <div class="col-sm-offset-5 col-sm-10">
        <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存</button>&nbsp;
        <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭</button>
    </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: datetimepicker-js"/>
<th:block th:include="include :: bootstrap-suggest-js"/>
<th:block th:include="include :: select2-css" />
<th:block th:include="include :: select2-js" />
<script th:src="@{/js/jquery.tmpl.js}"></script>
<script th:inline="javascript">
    $('.supplier-select').select2({
        placeholder: '--请选择供应商--',
        width:'100%',
        ajax: {
            url: '/warehouse/supplier/selectList',
            delay: 250,
            data: function (params) {
                return {
                    supplierName: params.term
                };
            }
        }
    })

    $(function () {
        var options = {
            pagination: false,
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            sidePagination: "client",
            columns: [{
                checkbox: true
            },
                {
                    field: 'index',
                    align: 'center',
                    title: "序号",
                    formatter: function (value, row, index) {
                        var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
                        var columnId = $.common.sprintf("<input type='hidden' name='goods[%s].id' value='%s'>", index, row.id);
                        return columnIndex + $.table.serialNumber(index) + columnId;
                    }
                },
                {
                    field: 'materialId',
                    align: 'center',
                    title: '物料编号',
                    formatter: function (value, row, index) {
                        return $.common.sprintf(
                            "<div class='col-sm-11' style='display: inline-flex'>" +
                            "<input name='goods[%s].materialId' class='form-control materialId' autocomplete='off' type='text' value='%s'>" +
                            "<div class='input-group-btn'>" +
                            "   <button type='button' class='btn btn-white dropdown-toggle' style='height: 31px' data-toggle='dropdown'>" +
                            "      <span class='caret'></span>" +
                            "   </button>" +
                            "   <ul class='dropdown-menu dropdown-menu-right' role='menu'>" +
                            "   </ul>" +
                            "</div></div>", index, value, index);
                    }
                },
                {
                    field: 'materialName',
                    align: 'center',
                    title: '物料名称',
                    formatter: function (value, row, index) {
                        return $.common.sprintf("<input class='form-control tc' type='text' name='goods[%s].materialName' value='%s' readonly>", index, value);
                    }
                },
                {
                    field: 'entryNum',
                    align: 'center',
                    title: '入库数量',
                    formatter: function (value, row, index) {
                        return $.common.sprintf("<input class='form-control entryNum tc' type='text' name='goods[%s].entryNum' value='%s'>", index, value);
                    }
                },
                {
                    field: 'unit',
                    align: 'center',
                    title: '单位',
                    formatter: function (value, row, index) {
                        return $.common.sprintf("<input class='form-control tc' type='text' name='goods[%s].unit' value='%s' readonly>", index, value);
                    }
                },
                {
                    field: 'unitPrice',
                    align: 'center',
                    title: '单价',
                    formatter: function (value, row, index) {
                        return $.common.sprintf("<input class='form-control unitPrice tc' type='text' name='goods[%s].unitPrice' value='%s'>", index, value);
                    }
                },
                {
                    field: 'taxAmount',
                    align: 'center',
                    title: '税额',
                    formatter: function (value, row, index) {
                        return $.common.sprintf("<input class='form-control taxAmount tc' type='text' name='goods[%s].taxAmount' value='%s'>", index, value);
                    }
                },
                {
                    field: 'totalPrice',
                    align: 'center',
                    title: '价税合计',
                    formatter: function (value, row, index) {
                        return $.common.sprintf("<input class='form-control tc' type='text' name='goods[%s].totalPrice' value='%s' readonly>", index, value);
                    }
                },
                {
                    field: 'producer',
                    align: 'center',
                    title: '产地',
                    formatter: function (value, row, index) {
                        return $.common.sprintf("<input class='form-control tc' type='text' name='goods[%s].producer' value='%s'>", index, value);
                    }
                },
                {
                    field: 'brand',
                    align: 'center',
                    title: '品牌',
                    formatter: function (value, row, index) {
                        return $.common.sprintf("<input class='form-control tc' type='text' name='goods[%s].brand' value='%s'>", index, value);
                    }
                },
                {
                    field: 'remarks',
                    align: 'center',
                    title: '备注',
                    formatter: function (value, row, index) {
                        return $.common.sprintf("<input class='form-control tc' type='text' name='goods[%s].remarks' value='%s'>", index, value);
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var value = $.common.isNotEmpty(row.index) ? row.index : $.table.serialNumber(index);
                        return '<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="sub.delRowByIndex(\'' + value + '\')"><i class="fa fa-remove"></i>删除</a>';
                    }
                }]
        };
        $.table.init(options);
    });

    var prefix = ctx + "warehouse/warehousingEntry"
    $("#form-warehousingEntry-add").validate({
        focusCleanup: true
    });

    /* 主子表-提交 */
    function submitHandler(index, layero) {
        if ($.validate.form()) {
            var data = $("#form-add").serializeArray();
            console.info(JSON.stringify(data));
            $.operate.saveTab(prefix + "/add", data);
        }
    }

    $("input[name='entryTime']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    function addRow() {
        var count = $("#" + table.options.id).bootstrapTable('getData').length;
        var row = {
            index: $.table.serialNumber(count),
            materialId: "",
            materialName: "",
            entryNum: "",
            unitPrice: "",
            taxAmount:"",
            unit: "",
            totalPrice: "",
            producer: "",
            brand: "",
            remarks: "",
        }
        sub.addRow(row);
    }

    $("#bootstrap-table").on("post-body.bs.table", function (e, args) {
        $('.unitPrice, .entryNum, .taxAmount').on('input', function (e) {
            let i = e.currentTarget.name.split('.')[0];
            let up = $("input[name='" + i + ".unitPrice']").val();
            let en = $("input[name='" + i + ".entryNum']").val();
            let ta = $("input[name='" + i + ".taxAmount']").val();
            $("input[name='" + i + ".totalPrice']").val(up * en + Number(ta))
        });

        $('.materialId').bsSuggest({
            idField: "materialId",      // 每组数据的哪个字段作为 data-id，优先级高于 indexId 设置（推荐）
            keyField: "materialId",   // 每组数据的哪个字段作为输入框内容，优先级高于 indexKey 设置（推荐）
            allowNoKeyword: false,      // 是否允许无关键字时请求数据
            hideOnSelect: true,         // 鼠标从列表单击选择了值时，是否隐藏选择列表
            multiWord: true,            // 以分隔符号分割的多关键字支持
            separator: ",",             // 多关键字支持时的分隔符，默认为空格
            getDataMethod: "url",       // 获取数据的方式，总是从 URL 获取
            effectiveFields: ["materialId", "materialName", "unit"],
            effectiveFieldsAlias: {
                'materialId': "物料编码",
                'materialName': "物料名称",
                'unit': '单位'
            },
            showHeader: true,
            url: '/warehouse/materialInfo/getMaterials?materialId='
        }).on("onSetSelectValue", function (e, selectedData, selectedRawData) {
            console.info("selectedRawData",selectedRawData)
            let i = e.currentTarget.name.split('.')[0];
            $("input[name='" + i + ".unit']").val(selectedRawData.unit);
            $("input[name='" + i + ".materialName']").val(selectedRawData.materialName);
        });
    });
</script>
</body>
</html>